<html>

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>第一个JSP程序</title>
    <script type="text/javascript" src="../jquery-1.8.3.js"></script>
    <script type="text/javascript">
        $(function() {
            $("#btn").click(function(){
                var username = $("#username").val();
                var password = $("#password").val();
                //验证
                $.getJSON("users.json").pipe(function(data){
                    if(checkUser(data,username,password)){
                        return $.getJSON("status.json");
                    } else {
                        //错误
                        var def = $.Deferred();
                        def.reject("用户名或者密码不正确");
                        return def;
                    }
                }).pipe(function(data){
                    if(checkStatus(data,username)) {
                        return username;
                    }else {
                        var def = $.Deferred();
                        def.reject("用户已经被停用");
                        return def;
                    }
                }).done(function(data) {
                    $("#content").html("欢迎你"+data+"登录");
                }).fail(function(data){
                    $("#content").html(data);
                });
            });
            function checkUser(data,username,password){
                for(var i=0;i<data.length;i++){
                    if(data[i]["name"]==username) {
                        if(data[i]["password"]==password) {
                            return true;
                        }
                    }
                }
                return false;
            };
            function checkStatus(data,username){
                for(var i=0;i<data.length;i++) {
                    if(data[i]["name"]==username){
                        if(data[i]["status"]==1) {
                            return true;
                        }
                    }
                }
                return false;
            };
        })
    </script>
</head>

<body>
    用户姓名:<input type="text" name="username" id="username"/><br/>
    用户密码:<input type="password" name="password" id="password"/><br/>
    <input type="button" value="用户验证" id="btn"/>
    <div id="content"></div>

</body>

</html>
